/*
 * Copyright (c) 2015 5 6
 * Name:wuPeiJi
 * QQ:757671834
 */

package kali.wpj.kali;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.johnpersano.supertoasts.SuperToast;
import com.gitonway.lee.niftymodaldialogeffects.lib.Effectstype;
import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;
import com.marshalchen.common.ui.ToastUtil;
import com.marshalchen.common.uimodule.activityanimation.AnimatedRectActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;
import kali.wpj.kali.util.IPTools;
import kali.wpj.kali.util.SharedTools;

/**
 * User: special
 * Date: 13-12-22
 * Time: 下午3:28
 * Mail: specialcyci@gmail.com
 */
public class SettingsFragment extends AnimatedRectActivity {
    @InjectView(R.id.ed_common_ip)
    EditText ed_common_ip;
    @InjectView(R.id.btn_common_sure)
    Button btn_common_sure;
    @InjectView(R.id.btn_common_cancle)
    Button btn_common_cancle;
//    @InjectView(R.id.ed_search_ip)
//    EditText ed_search_ip;
//    @InjectView(R.id.btn_whois_ip)
    Button btn_whois_ip;
//    @InjectView(R.id.txt_whois_result)
//    TextView txt_whois_result;
    private String IP;//whois模块ip（数字）
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {

          //  txt_whois_result.setText("IP:"+msg.obj.toString());
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.inject(this);
        btn_common_sure.setOnClickListener(btnOnclickerListener);
        btn_common_cancle.setOnClickListener(btnOnclickerListener);
      //  btn_whois_ip.setOnClickListener(btnOnclickerListener);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_setting;
    }

    View.OnClickListener btnOnclickerListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_common_sure :
                    boolean isIP= IPTools.isIPAddress(ed_common_ip.getText().toString().trim());
                    if(!isIP){
                        dialogShow(getString(R.string.inPuterr));
                        ed_common_ip.setText("");
                        return ;
                    }
                    Log.i("iSIP", isIP + "-----" + ed_common_ip.getText().toString());
                    boolean result=  writeIpToPhone(SettingsFragment.this,"ip",ed_common_ip.getText().toString());
                    if(result){
                        SuperToast superToast = new SuperToast(SettingsFragment.this);
                        superToast.setDuration(SuperToast.Duration.LONG);
                        superToast.setText(getString(R.string.setsuccess));
                        superToast.setIcon(SuperToast.Icon.Dark.INFO, SuperToast.IconPosition.LEFT);
                        superToast.show();

                    }else{
                        dialogShow("失败");
                    }
                    break;
                case R.id.btn_common_cancle :
                    returnMain(SettingsFragment.this, MainActivity.class);
                    break;
//                case  R.id.btn_whois_ip:
//                    new Thread(runnable).start();
            }

        }
    };
    private boolean writeIpToPhone(Context context,String key,String value) {
        boolean result=  SharedTools.putString(context, key, value);
        return result;
    }
    private void returnMain(Context context,Class clazz){
        Intent intent=new Intent(context,clazz);
        startActivity(intent);
        overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_in_top);
        SettingsFragment.this.finish();
    }

    Runnable runnable=new Runnable() {
        String tem=null;
        @Override
        public void run() {
          //  tem=IPTools.getIPByHostName(ed_search_ip.getText().toString());
//            System.out.println("Setting:"+tem);
//            Message msg=new Message();
//            msg.obj=tem;
//            handler.sendMessage(msg);
        }

    };
    public void dialogShow(String str){
        NiftyDialogBuilder dialogBuilder=NiftyDialogBuilder.getInstance(SettingsFragment.this);
        dialogBuilder
                .withTitle("提醒")                                  //.withTitle(null)  no title
                .withTitleColor("#FFFFFF")                                  //def
                .withDividerColor("#11000000")                              //def
                .withMessage(str)                     //.withMessage(null)  no Msg
                .withMessageColor("#FFFFFFFF")                              //def  | withMessageColor(int resid)
                .withDialogColor(getString(R.string.black))                               //def  | withDialogColor(int resid)                               //def
                .isCancelableOnTouchOutside(true)                           //def    | isCancelable(true)
                .withDuration(700)                                          //def
                .withEffect(Effectstype.RotateBottom)
                .withIcon(getResources().getDrawable(R.drawable.warn))//def Effectstype.Slidetop
                .setCustomView(R.layout.custom_view,this)         //.setCustomView(View or ResId,context)
                .show();

    }

}
